extension ListExtension<T> on List<T> {
  T? firstWhereOrNull(bool Function(T element) condition) {
    for (final element in this) {
      if (condition(element)) return element;
    }
    return null;
  }
}

enum ActivityType {
  running(1, 'Running'),

  climbing(2, 'Climbing'),

  hiking(5, 'Hiking'),

  cycling(7, 'Cycling'),

  ski(10, 'Skiing');

  const ActivityType(this.number, this.value);

  final int number;

  final String value;
  static ActivityType? getTypeByTitle(String title) => ActivityType.values
      .firstWhereOrNull((activity) => activity.name == title);

  static ActivityType? getType(int number) => ActivityType.values
      .firstWhereOrNull((activity) => activity.number == number);

  static String? getValue(int number) => ActivityType.values
      .firstWhereOrNull((activity) => activity.number == number)
      ?.value;
}

// 类型
enum SnippetType {
  work('工作', '1', 'work'),
  live('生活', '2', 'live'),
  study('学习', '3', 'study'),
  code('代码', '4', 'code'),
  note('备忘', '5', 'note');

  // 构造器
  const SnippetType(this.label, this.value, this.name);
  final String label;
  final String value;
  final String name;

  static SnippetType? getTypeByTitle(String title) =>
      SnippetType.values.firstWhereOrNull((type) => type.name == title);

  static SnippetType? getType(String label) =>
      SnippetType.values.firstWhereOrNull((type) => type.label == label);

  static String? getValue(String label) =>
      SnippetType.values.firstWhereOrNull((type) => type.label == label)?.value;

  static String? getLabel(String value) =>
      SnippetType.values.firstWhereOrNull((type) => type.value == value)?.label;

  static String? getLabelByName(String name) =>
      SnippetType.values.firstWhereOrNull((type) => type.name == name)?.label;

  static String? getNameByLabel(String label) =>
      SnippetType.values.firstWhereOrNull((type) => type.label == label)?.name;
}

//是否提醒（0否 1是）
enum IsRemind {
  yes('是', true),
  no('否', false);

  const IsRemind(this.label, this.value);
  final String label;
  final bool value;

  static IsRemind? getTypeByTitle(String title) =>
      IsRemind.values.firstWhereOrNull((isRemind) => isRemind.name == title);

  static IsRemind? getType(String label) =>
      IsRemind.values.firstWhereOrNull((isRemind) => isRemind.label == label);

  static bool? getValue(String label) => IsRemind.values
      .firstWhereOrNull((isRemind) => isRemind.label == label)
      ?.value;

  static String? getLabel(bool value) => IsRemind.values
      .firstWhereOrNull((isRemind) => isRemind.value == value)
      ?.label;

  String isRemindString(String label) => '$label is $value';
}

//等级 0一般 1优先 2紧急
enum Level {
  general('一般', 0),
  priority('优先', 1),
  emergency('紧急', 2);

  const Level(this.label, this.value);
  final String label;
  final int value;

  static Level? getTypeByTitle(String title) =>
      Level.values.firstWhereOrNull((level) => level.name == title);

  static Level? getType(String label) =>
      Level.values.firstWhereOrNull((level) => level.label == label);

  static int? getValue(String label) =>
      Level.values.firstWhereOrNull((level) => level.label == label)?.value;

  static String? getLabel(int value) =>
      Level.values.firstWhereOrNull((level) => level.value == value)?.label;
}

//状态（false 未完成 true已完成）
enum ItemStatus {
  notDone('未完成', false),
  done('已完成', true);

  const ItemStatus(this.label, this.value);
  final String label;
  final bool value;

  static ItemStatus? getTypeByTitle(String title) => ItemStatus.values
      .firstWhereOrNull((itemStatus) => itemStatus.name == title);

  static ItemStatus? getType(String label) => ItemStatus.values
      .firstWhereOrNull((itemStatus) => itemStatus.label == label);

  static bool? getValue(String label) => ItemStatus.values
      .firstWhereOrNull((itemStatus) => itemStatus.label == label)
      ?.value;

  static String? getLabel(bool value) => ItemStatus.values
      .firstWhereOrNull((itemStatus) => itemStatus.value == value)
      ?.label;
}
